package cn.tedu.csmall.product.service.impl;

import cn.tedu.csmall.commons.ex.ServiceException;
import cn.tedu.csmall.commons.pojo.vo.PageData;
import cn.tedu.csmall.commons.util.PageInfoToPageDataConverter;
import cn.tedu.csmall.commons.web.ServiceCode;
import cn.tedu.csmall.product.mapper.AlbumMapper;
import cn.tedu.csmall.product.mapper.PictureMapper;
import cn.tedu.csmall.product.pojo.entiry.Album;
import cn.tedu.csmall.product.pojo.entiry.Picture;
import cn.tedu.csmall.product.pojo.param.PictureAddNewParam;
import cn.tedu.csmall.product.pojo.vo.AlbumStandardVO;
import cn.tedu.csmall.product.pojo.vo.PictureStandardVO;
import cn.tedu.csmall.product.service.IPictureService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class PictureServiceImpl implements IPictureService {

    @Autowired
    private PictureMapper pictureMapper;
    @Autowired
    private AlbumMapper albumMapper;

    @Override
    public void addNewPicture(PictureAddNewParam pictureAddNewParam) {
        log.debug("开始处理【添加图片】的业务，参数：{}", pictureAddNewParam);

        // 检查所添加图片的相册是否存在，若不存在，则抛出异常
        QueryWrapper<Album> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", pictureAddNewParam.getAlbumId());
        int countByAlbumId = albumMapper.selectCount(queryWrapper);
        if (countByAlbumId == 0) {
            String message = "添加图片失败，相册数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        //将相册数据添加到数据库
        Picture picture = new Picture();
        BeanUtils.copyProperties(pictureAddNewParam, picture);
        picture.setIsCover(0);
        picture.setGmtCreate(LocalDateTime.now());
        picture.setGmtModified(LocalDateTime.now());
        int rows = pictureMapper.insert(picture);
        if (rows != 1) {
            String message = "添加图片失败，服务器正忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
        log.debug("新增【图片ID=" + picture.getId() + "】成功！");

    }

    @Override
    public void deletePicture(Long pictureId) {
        log.debug("开始处理【根据ID删除图片】的业务，参数：{}", pictureId);

        // 检查要删除的图片存不存在，若不存在，则抛出异常
        QueryWrapper<Picture> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", pictureId);
        int countById = pictureMapper.selectCount(queryWrapper);
        if (countById == 0) {
            String message = "删除图片失败，数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        // 执行删除
        int rows = pictureMapper.deleteById(pictureId);
        if (rows != 1) {
            String message = "删除图片失败，服务器正忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }
        log.debug("删除【图片ID={}】完成！", pictureId);


    }

    @Override
    public void updateSetCover(Long id) {
        log.debug("开始处理【通过id将图片设置为相册封面】的业务，图片id：{}", id);

        // 检查图片是否存在，不存在则抛出异常
        PictureStandardVO standard = pictureMapper.getStandard(id);
        if (standard == null) {
            String message = "设置封面失败，图片数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        // 检查图片是否已经是封面，若是，则抛出异常
        if (standard.getIsCover() == 1) {
            String message = "设置封面失败，此图片已经是相册封面！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }

        // 检查相册是否存在，不存在则抛出异常
        // 先获取图片信息，找到对应相册id，再去查是否存在此相册
        Long albumId = standard.getAlbumId();
        AlbumStandardVO albumStandardById = albumMapper.getAlbumStandardById(albumId);
        if (albumStandardById == null) {
            String message = "设置封面失败，相册数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        // 将此相册的所有图片设置为“非封面”
        Picture picture = new Picture();
        picture.setIsCover(0);
        QueryWrapper<Picture> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("album_id", albumId);
        int rows = pictureMapper.update(picture, queryWrapper);
        log.debug("将id为：{}的相册的图片全部设为非封面完成，共修改了{}行数据！", albumId, rows);

        // 将图片设置为封面
        Picture thePicture = new Picture();
        thePicture.setId(id);
        thePicture.setIsCover(1);
        int rows1 = pictureMapper.updateById(thePicture);
        if (rows1 != 1) {
            String message = "设置封面失败，服务器正忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }
        log.debug("将图片设置为封面完成！");

    }


}
